package cn.tedu.sp06.controller;


import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.web.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class RibbonController {

    @Autowired
    private RestTemplate rt;

    @GetMapping("/item-service/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId){
        return rt.getForObject("http://item-service/{1}", JsonResult.class,orderId);
    }

    @PostMapping("/item-service/decreaseNumber")
    public JsonResult decreaseNumber(@RequestBody List<Item> items) {
        return rt.postForObject("http://item-service/decreaseNumber", items,JsonResult.class);
    }

    @GetMapping("/user-service/{userId}/score")
    public JsonResult<User> addScore(@PathVariable Integer userId,
                                     @RequestParam Integer score){
        return rt.getForObject("http://user-service/{1}/score?score={2}", JsonResult.class,userId,score);
    }
    @GetMapping("/order-service/{orderId}")
    public JsonResult<Order> getOrder(@PathVariable String orderId){
        return rt.getForObject("http://order-service/{1}", JsonResult.class,orderId);
    }
    @GetMapping("/order-service")
    public JsonResult addOrder(){
        return rt.getForObject("http://order-service/", JsonResult.class);
    }
}
